package org.kaindorf.kainflix.metadata.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kaindorf.kainflix.metadata.exceptions.ResourceNotFoundException;
import org.kaindorf.kainflix.metadata.exceptions.IllegalDataGivenException;
import org.kaindorf.kainflix.metadata.repository.entity.Metadata;
import org.kaindorf.kainflix.metadata.service.MetaDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest/metadata")
public class MetadataRestController {

    private static final Logger logger = LogManager.getLogger(MetadataRestController.class);
    
    @Autowired
    private MetaDataService metaDataService;

    @RequestMapping(method = RequestMethod.POST, consumes = {"application/json", "text/xml", "application/xml"}, produces = "text/plain")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String create(@RequestBody Metadata metaData) {
        if (metaData.getId() != null) {
            throw new IllegalDataGivenException("ID must NOT be given / must be null");
        }
        Long metadataId = metaDataService.saveMetaData(metaData).getId();
        String response = Long.toString(metadataId);
        return response;
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public void update(@PathVariable("metaDataId") long metaDataId, @RequestBody Metadata metaData) {
        if (metaData.getId() != null) {
            throw new IllegalDataGivenException("ID in body must NOT be given / must be null");
        }
        metaData.setId(metaDataId);
        metaDataService.updateMetaData(metaData);
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public void delete(@PathVariable("metaDataId") long metaDataId) {
        metaDataService.delete(metaDataId);
    }

    @RequestMapping(value = "{metaDataId}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public Metadata read(@PathVariable("metaDataId") long metaDataId) {
        return metaDataService.getMetaDataById(metaDataId);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public Iterable<Metadata> readAll() {
        return metaDataService.getAllMetaData();
    }
    
    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleResourceNotFoundException(ResourceNotFoundException ex){
        logger.debug("some non-existing metadata has been requested");
        return "Your requested resource could not be found. Error message is: " + ex.getMessage();
    }
}
